package cn.imvc.app.models;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.domains.OrderType;
import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.MemberRed;
import cn.imvc.app.entities.Red;
import cn.imvc.app.repositories.MemberRedRepository;
import cn.imvc.app.repositories.RedRepository;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.util.RandomId;

@Service
public class RedModelImpl implements RedModel{
	
	@Autowired
	private RedRepository redRepository;

	
	
	public Red get(int id){
		return redRepository.findOne(id);
	}
	
	
	public Page<Red> find(ViewRequest viewRequest) {
		return (Page<Red>) redRepository.findAll();
	}

	public Red save(Red red) {
		
		Red red2 = redRepository.saveAndFlush(red);
		
		
		return red2;
	}
	public Red update(Red newRed) {
		Red loadRed=get(newRed.getId());
		BeanUtils.copyProperties(newRed, loadRed, new String[]{"id"});
		
		return redRepository.saveAndFlush(loadRed);
	}
	public void delete(int id) {
		redRepository.delete(id);
		
	}
	public void deleteBatch(int[] ids) {

		for (int id : ids) {
			delete(id);
		}
		
	}

	public List<Red> findByTime(Date date) {
		return redRepository.findByEndTimeGreaterThanOrderByStartTime(new Date());
	}



}
